﻿Imports Microsoft.WindowsMobile
Imports Microsoft.WindowsMobile.PocketOutlook
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception
Imports System.Data.SqlServerCe

Public Class EventAdd

    Dim iEventCode As Integer

    Private Sub MenuItemBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemBack.Click
        HomeForm.Show()
        Hide()
    End Sub

    Private Sub EventAdd_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshContacts()

        iEventCode = Rnd(100) * 100

        'TODO: You would want to clean the inputs every time this form is open
        AMPMListBox.SelectedIndex = 0
    End Sub

    Private Sub EventView_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        HomeForm.Show()
    End Sub

    Private Sub FromMessage_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FromMessage.TextChanged
        FullMessage.Text = FromMessage.Text + "- plz reply 'SMSRSVP " + iEventCode.ToString() + "' to RSVP."
        MessageFinal.Text = FullMessage.Text
    End Sub

    Private Sub AddContact_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddContact.Click
        For Each contactItem As ListViewItem In ContactsListBox.Items
            If contactItem.Selected Then
                Dim newContactItem As New ListViewItem(contactItem.Text)
                Dim newContactItem2 As New ListViewItem(contactItem.Text)

                InviteesListBox.Items.Add(newContactItem)
                ListBoxFinalReceipients.Items.Add(newContactItem2)
            End If
        Next
        RefreshContacts()
    End Sub

    Private Sub RemoveContact_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveContact.Click
        For Each contactItem As ListViewItem In InviteesListBox.Items
            If contactItem.Selected Then
                InviteesListBox.Items.Remove(contactItem)
                ListBoxFinalReceipients.Items.Remove(contactItem)
            End If
        Next
        RefreshContacts()
    End Sub

    Private Sub RefreshContacts()
        ContactsListBox.Items.Clear()

        Dim poom As Microsoft.WindowsMobile.PocketOutlook.OutlookSession
        poom = New OutlookSession

        For Each contact As Contact In poom.Contacts.Items
            Dim contactItem As New ListViewItem
            contactItem.Text = contact.FirstName() & " " & contact.LastName()

            If Not (InviteesListBox.Items.Contains(contactItem)) Then
                ContactsListBox.Items.Add(contactItem)
            End If
        Next

    End Sub


    Private Sub ButtonSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSend.Click
        'Assemble our date & time
        Dim eventDate As DateTime
        eventDate = Convert.ToDateTime(EventDateTimePicker.Value.ToShortDateString() & " " & Convert.ToString(EventHourNumericUpDown.Value) & ":" & Convert.ToString(EventMinuteNumericUpDown.Value) & " " & Convert.ToString(AMPMListBox.SelectedValue))


        'Add the event to the DB
        SaveEvent(iEventCode, eventDate, EventNameTextBox.Text, EventLocationTextBox.Text, MessageFinal.Text)

        'Loop though all the contacts and SMS them
        SendInvites(MessageFinal.Text)

        'TODO: Save a record of invited to RSVPs table.

        'MsgBox("All Done!")

        'Refresh data on the home screen and show it
        HomeForm.EventsTableAdapter.Fill(HomeForm.SMSRSVPDataSet.Events)
        HomeForm.Show()
        Hide()
    End Sub

    Private Sub SaveEvent(ByVal EventCode As String, ByVal EventDate As Date, ByVal EventName As String, ByVal Location As String, ByVal Message As String)

        Dim cnn As SqlCeConnection = New SqlCeConnection("Data Source=\Program Files\SMSRSVP\SMSRSVP.sdf;")
        Dim cmd As SqlCeCommand = New SqlCeCommand
        cmd.Connection = cnn
        cnn.Open()

        cmd.CommandText = "INSERT INTO Events([EventCode],[EventDate],[EventName],[EventLocation],[EventMessage])" + _
                                    " VALUES('" + EventCode + "','" + EventDate.ToString() + "','" + EventName + "','" + Location + "','" + Message.Replace("'", "''") + "')"

        Try 'In case we already have this item we can't insert, so we will ingnore for simplicity

            cmd.ExecuteNonQuery()

        Catch ex As Exception
            'TODO: Notify user
        End Try

        'Clean up our connections
        cmd.Dispose()
        cnn.Dispose()

    End Sub

    Private Sub SendInvites(ByVal Message As String)

        For Each invitee As ListViewItem In InviteesListBox.Items

            Dim poom As Microsoft.WindowsMobile.PocketOutlook.OutlookSession
            poom = New OutlookSession

            For Each contact As Contact In poom.Contacts.Items
                If invitee.Text = contact.FirstName + " " + contact.LastName Then
                    Dim msg As New SmsMessage()
                    msg.To.Add(New Recipient(contact.MobileTelephoneNumber))
                    msg.Body = Message
                    msg.Send()
                End If
            Next

        Next

    End Sub


    'TODO: Show softkey button for Next and change to Send on last screen.

    'Private Sub MenuItemForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemForward.Click
    '    If (TabPageEventInfo.Focused) Then
    '        TabPageMessage.Focus()
    '    End If
    '    If (TabPageMessage.Focused) Then
    '        TabPageInvite.Focus()
    '    End If
    '    If (TabPageInvite.Focused) Then
    '        TabPageReview.Focus()
    '        MenuItemForward.Enabled = False
    '        MenuItemForward.Text = "Use Send"
    '    End If
    '    If (TabPageReview.Focused) Then

    '    End If
    'End Sub

    'Private Sub TabControlEvent_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControlEvent.SelectedIndexChanged
    '    MenuItemForward.Enabled = True
    '    MenuItemForward.Text = "Next"

    '    If (TabPageMessage.Focused) Then

    '        MessageFinal.Text = FullMessage.Text

    '    End If
    '    If (TabPageInvite.Focused) Then

    '        RefreshContacts()

    '    End If
    '    If (TabPageReview.Focused) Then

    '        MenuItemForward.Enabled = False
    '        MenuItemForward.Text = "Use Send"
    '    End If
    'End Sub

End Class